3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @subpackage SpecialPage
27 var $reportingInterval = 100;
28 var $reporting = true;
31 var $server = null; // use default
32 var $pages = null; // all pages
33 var $skipHeader = false; // don't output <mediawiki> and <siteinfo>
34 var $skipFooter = false; // don't output </mediawiki>
37 var $sink = null; // Output filters
38 var $stubText = false; // include rev_text_id instead of text; for 2-pass dump
40 function BackupDumper( $args ) {
41 $this->stderr = fopen( "php://stderr", "wt" );
42 $this->sink = $this->processArgs( $args );
50 function processArgs( $args ) {
52 'file' => 'DumpFileOutput',
53 'gzip' => 'DumpGZipOutput',
54 'bzip2' => 'DumpBZip2Output',
55 '7zip' => 'Dump7ZipOutput' );
57 'latest' => 'DumpLatestFilter',
58 'notalk' => 'DumpNotalkFilter',
59 'namespace' => 'DumpNamespaceFilter' );
62 foreach( $args as $arg ) {
63 if( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
64 @list( $full, $opt, $val, $param ) = $matches;
67 if( !is_null( $sink ) ) {
70 if( !isset( $outputTypes[$val] ) ) {
71 die( "Unrecognized output sink type '$val'\n" );
73 $type = $outputTypes[$val];
74 $sink = new $type( $param );
77 if( is_null( $sink ) ) {
78 $this->progress( "Warning: assuming stdout for filter output\n" );
79 $sink = new DumpOutput();
81 if( !isset( $filterTypes[$val] ) ) {
82 die( "Unrecognized filter type '$val'\n" );
84 $type = $filterTypes[$val];
85 $filter = new $type( $sink, $param );
87 // references are lame in php...
93 $this->processOption( $opt, $val, $param );
98 if( is_null( $sink ) ) {
99 $sink = new DumpOutput();
103 if( count( $sinks ) > 1 ) {
104 return new DumpMultiWriter( $sinks );
110 function processOption( $opt, $val, $param ) {
111 // extension point for subclasses to add options
114 function dump( $history, $text = MW_EXPORT_TEXT ) {
115 # This shouldn't happen if on console... ;)
116 header( 'Content-type: text/html; charset=UTF-8' );
118 # Notice messages will foul up your XML output even if they're
119 # relatively harmless.
120 ini_set( 'display_errors', false );
122 $this->startTime = wfTime();
124 $dbr =& wfGetDB( DB_SLAVE );
125 $this->maxCount = $dbr->selectField( 'page', 'MAX(page_id)', '', 'BackupDumper::dump' );
126 $this->startTime = wfTime();
128 $db =& $this->backupDb();
129 $exporter = new WikiExporter( $db, $history, MW_EXPORT_STREAM, $text );
131 $wrapper = new ExportProgressFilter( $this->sink, $this );
132 $exporter->setOutputSink( $wrapper );
134 if( !$this->skipHeader )
135 $exporter->openStream();
137 if( is_null( $this->pages ) ) {
138 if( $this->startId || $this->endId ) {
139 $exporter->pagesByRange( $this->startId, $this->endId );
141 $exporter->allPages();
144 $exporter->pagesByName( $this->pages );
147 if( !$this->skipFooter )
148 $exporter->closeStream();
150 $this->report( true );
153 function &backupDb() {
154 global $wgDBadminuser, $wgDBadminpassword;
156 $db =& new Database( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname );
157 $timeout = 3600 * 24;
158 $db->query( "SET net_read_timeout=$timeout" );
159 $db->query( "SET net_write_timeout=$timeout" );
163 function backupServer() {
170 function reportPage() {
175 function revCount() {
179 function report( $final = false ) {
180 if( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
185 function showReport() {
186 if( $this->reporting ) {
187 $delta = wfTime() - $this->startTime;
188 $now = wfTimestamp( TS_DB );
190 $rate = $this->pageCount / $delta;
191 $revrate = $this->revCount / $delta;
192 $portion = $this->pageCount / $this->maxCount;
193 $eta = $this->startTime + $delta / $portion;
194 $etats = wfTimestamp( TS_DB, intval( $eta ) );
201 $this->progress( "$now: $wgDBname $this->pageCount, ETA $etats ($rate pages/sec $revrate revs/sec)" );
205 function progress( $string ) {
206 fwrite( $this->stderr, $string . "\n" );
210 class ExportProgressFilter extends DumpFilter {
211 function ExportProgressFilter( &$sink, &$progress ) {
212 parent::DumpFilter( $sink );
213 $this->progress = $progress;
216 function writeClosePage( $string ) {
217 parent::writeClosePage( $string );
218 $this->progress->reportPage();
221 function writeRevision( $rev, $string ) {
222 parent::writeRevision( $rev, $string );
223 $this->progress->revCount();